package com.wang.project.consumer.payment;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.project.email.EmailPayMessageHandle;
import com.wang.project.model.entity.UserInterfaceInfo;
import com.wang.project.model.entity.UserInterfacePayment;
import com.wang.project.service.UserInterfaceInfoService;
import com.wang.project.service.UserInterfacePaymentService;
import com.wang.project.constant.PaymentEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * consumerGroup 配置暂时没什么用，但是必须要配置
 */
@Service
@Slf4j
@RocketMQMessageListener(
        topic = "payment_addInterfaceNum_result_topic",
        consumerGroup = "payment-interface-group"
)
public class paymentAddInterfaceNumResultConsumer implements RocketMQListener<Map<String, String>> {


    @Resource
    private UserInterfacePaymentService userInterfacePaymentService;

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    @Resource
    private EmailPayMessageHandle emailPayMessageHandle;

    private final static Integer addInterfaceNum = 50; //支付一次增加的接口调用次数


    /**
     * 更改用户的支付态，不用刷洗ak sk
     * 支付宝中已经做了幂等性的处理，这里就不需要再做幂等处理了
     *
     * @param params
     */
    @Override
    public void onMessage(Map<String, String> params) {
        log.info("paymentAddInterfaceNumResultConsumer success Map params: " + JSONUtil.toJsonStr(params));
        //更改用户的支付态
        /**
         * todo 暂时不知道支付成功的调用中有什么参数，猜测应该有支付单号  支付单号的key也不知道是什么，暂定为model中的 outTradeNo
         */
        String paymentOrder = params.get("outTradeNo");
        QueryWrapper<UserInterfacePayment> paymentQueryWrapper = new QueryWrapper<>();
        paymentQueryWrapper.eq("orderId", paymentOrder);
        UserInterfacePayment userInterfacePayment = userInterfacePaymentService.getOne(paymentQueryWrapper);
        userInterfacePayment.setPayStatus(PaymentEnum.Paid.getCode());

        //更新支付表中的支付状态
        boolean updatePayStatus = userInterfacePaymentService.save(userInterfacePayment);
        if (!updatePayStatus) {
            log.info("paymentAddInterfaceNumResultConsumer onMessage 更新支付状态失败");
        }

        //更新用户接口表的信息 添加50次接口调用次数
        //先查出用户接口表的信息
        QueryWrapper<UserInterfaceInfo> userInterfaceInfoQueryWrapper = new QueryWrapper<>();
        userInterfaceInfoQueryWrapper.eq("userId", userInterfacePayment.getUserId());
        userInterfaceInfoQueryWrapper.eq("interfaceId", userInterfacePayment.getInterfaceId());
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoService.getOne(userInterfaceInfoQueryWrapper);
        userInterfaceInfo.setTotalNum(userInterfaceInfo.getTotalNum() + addInterfaceNum);
        userInterfaceInfo.setLeftNum(userInterfaceInfo.getLeftNum() + addInterfaceNum);

        boolean saveUserInterface = userInterfaceInfoService.save(userInterfaceInfo);
        if (!saveUserInterface) {
            log.info("paymentAddInterfaceNumResultConsumer onMessage 增加接口调用次数更新数据失败，请尝试手动更新数据");
        }
        log.info("用户：{} 完成支付流程 end...", userInterfacePayment.getUserId());

        //邮件通知 通知什么 订单号
        emailPayMessageHandle.sendPayMessage(paymentOrder,userInterfacePayment.getUserId());
    }
}
